<?php

namespace app\common\model;

use think\Model;

/**
 * 申请成为代理
 * 字段：
 * agent_application_id:这份申请的标识符
 * location:申请的地区
 * comments：申请的备注
 * application_status：申请的状态，未审核，已通过，未通过
 * 外部关系：
 * user:发起申请人
 * confirm_user:批准申请人.
 */
class AgentApplication extends Model
{
    public function user()
    {
        return $this->belongsTo('User', 'user_id');
    }
    public function confirmed_user()
    {
        return $this->belongsTo('User', 'confirm_user', 'user_id');
    }
    public static $application_status_category = [0 => '未审核', 1 => '已通过', 2 => '未通过'];
    public function getApplicationStatusAttr($value)
    {
        return self::$application_status_category[$value];
    }
    public function setApplicationStatusAttr($value, $data)
    {
        $catedata = array_flip(self::$application_status_category);
        $this->confirm_time = date('Y-m-d H:i:s');
        if ($value == '已通过' and $data['application_status'] != 1) {
            $this->user->save(['user_type' => 'agent']);
            $this->user->owned_line->save(['line_name' => $data['location'].'服务站']);
        }
        if ($value != '已通过' and $data['application_status'] == 1) {
            $this->user->save(['user_type' => 'user']);
        }

        return $catedata[$value];
    }
}
